// import XLSX from "xlsx";
import * as XLSX from 'xlsx'
import { ElMessage } from 'element-plus'

/*
    * @description:
    * @param {Object} json 服务端发过来的数据
    * @param {String} name 导出Excel文件名字
   
    * @param {String} titleArr 导出Excel表头
   
    * @param {String} sheetName 导出sheetName名字
    * @return:
    */
export function EXPORTEXEL(json: any, name: any, titleArr: any, sheetName: any) {
    /* convert state to workbook */
    var data = new Array();
    var keyArray = new Array();
    const getLength = function (obj: any) {
        var count = 0;
        for (var i in obj) {
            if (obj.hasOwnProperty(i)) {
                count++;
            }
        }
        return count;
    };
    for (const key1 in json) {
        if (json.hasOwnProperty(key1)) {
            const element = json[key1];
            var rowDataArray = new Array();
            for (const key2 in element) {
                if (element.hasOwnProperty(key2)) {
                    const element2 = element[key2];
                    rowDataArray.push(element2);
                    if (keyArray.length < getLength(element)) {
                        keyArray.push(key2);
                    }
                }
            }
            data.push(rowDataArray);
        }
    }
    // keyArray为英文字段表头
    data.splice(0, 0, keyArray, titleArr);
    console.log('data', data);
    const ws = XLSX.utils.aoa_to_sheet(data);
    const wb = XLSX.utils.book_new();
    // 此处隐藏英文字段表头
    var wsrows = [{ hidden: true }];
    ws['!rows'] = wsrows; // ws - worksheet
    XLSX.utils.book_append_sheet(wb, ws, sheetName);
    /* generate file and send to client */
    XLSX.writeFile(wb, name + '.xlsx');
}
export function EXELTABLE(tableColumns: any, dataList: any, filename: string) {
    try {
        let titleColum = {
            titleArr: [],
            colunm: [],
        }
        tableColumns.reduce((pre: any, cur: { title: never, key: never, headerName: never, field: never }, index: any) => {
            if (cur.title != "操作" && cur.headerName != "操作") {
                if (cur.title) titleColum.titleArr.push(cur.title)//拿取title放进数组
                if (cur.key) titleColum.colunm.push(cur.key)//拿取key放进数组
                if (cur.headerName) titleColum.titleArr.push(cur.headerName)//拿取title放进数组
                if (cur.field) titleColum.colunm.push(cur.field)//拿取key放进数组
            }
        }, {})
        titleColum.colunm = titleColum.colunm.filter((item: any) => item != 'defaultCkBox')
        let head = ''
        titleColum.titleArr.map((item: any) => {
            head += `<th>${item}</th>`//头部
        })
        let data = dataList.reduce((pre: any, cur: any, index: any) => {
            let body = ''
            titleColum.colunm.map((item: string) => {
                body += `<td style="mso-number-format:'\\@'">${cur[item] ? cur[item] : ''}</td>`//身体
            })
            return pre += `<tr>${body}</tr>`
        }, '')
        //形成一个html
        const uri = 'data:application/vnd.ms-excel;base64,',
            template = `<html>
                <head><meta charset="UTF-8"></head>
                <body><table border="1"><tr>${head}</tr>${data}</table></body>
            </html>`
        console.log(titleColum)
        //模拟点击事件进行下载
        const a = document.createElement('a')
        a.href = uri + window.btoa(unescape(encodeURIComponent(template)))
        a.download = filename
        a.click()
        //下载之后清除
        a.remove()
    }

    catch (e: any) {
        // 报错提示
        ElMessage.error(e.name + ':' + ' ' + e.message)
    }
}